package cn.fzhui.servlet;

import cn.fzhui.factory.ServiceFactory;
import cn.fzhui.model.Student;
import cn.fzhui.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class StoreStudentServlet extends AuthServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        StudentService studentService = ServiceFactory.getStudentService();
        int result = 0;
        try {
            result = studentService.store(new Student(
                    req.getParameter("number"),
                    req.getParameter("name"),
                    req.getParameter("sex"),
                    req.getParameter("birthday"),
                    req.getParameter("email"),
                    req.getParameter("remark"),
                    req.getParameter("password"),
                    Integer.parseInt(req.getParameter("age"))
            ));

            // 返回列表页面
            resp.sendRedirect("/index");
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
    }

    /**
     * 与POST操作保持一致
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

}

